<?php
require_once "classdb.php";

// class tin **********************************************************************************
class tin extends db{
	private $table="tin";
	public $id=NULL;
	public $ten_vi=NULL;
	public $tomtat_vi=NULL;
	public $noidung_vi=NULL;
	public $ten_en=NULL;
	public $tomtat_en=NULL;
	public $noidung_en=NULL;
	public $url_img=NULL;
	public $url_file=NULL;
	public $ngay=NULL;
	public $thutu=-1;
	public $anhien=NULL;
	public $idtl=NULL;
	public $idloai=NULL;
	
	function dt_set_id($id) { $this->id=$id; }
	function dt_set_ten_vi($ten_vi) { $this->ten_vi=$ten_vi; }
	function dt_set_tomtat_vi($tomtat_vi) { $this->tomtat_vi=$tomtat_vi; }
	function dt_set_noidung_vi($noidung_vi) { $this->noidung_vi=$noidung_vi; }
	function dt_set_ten_en($ten_en) { $this->ten_en=$ten_en; }
	function dt_set_tomtat_en($tomtat_en) { $this->tomtat_en=$tomtat_en; }
	function dt_set_noidung_en($noidung_en) { $this->noidung_en=$noidung_en; }
	function dt_set_url_img($url_img) { $this->url_img=$url_img; }
	function dt_set_url_file($url_file) { $this->url_file=$url_file; }
	function dt_set_ngay($ngay) { $this->ngay=$ngay; }
	function dt_set_thutu($thutu) { $this->thutu=$thutu; }
	function dt_set_anhien($anhien) { $this->anhien=$anhien; }
	function dt_set_idtl($idtl) { $this->idtl=$idtl; }
	function dt_set_idloai($idloai) { $this->idloai=$idloai; }
	
	
	function get_by_thutu($id=-1,$anhien=-1) {
        return $this->get_by_thutu_db($this->table,$id,$anhien);
	}
	function get_by_ngay($id=-1,$anhien=-1) {
		return $this->get_by_ngay_db($this->table,$id,$anhien);
	}
    
	function change_anhien($id) {
	   	$this->change_anhien_db($this->table,$id);
    }
	function change_anhien_list($listid) {
	   	$this->change_anhien_list_db($this->table,$listid);
    }
    
	function delete($id) {
		$this->delete_data_db($this->table,$id);
	}
	function delete_list($listid) {
		$this->delete_list_data_db($this->table,$listid);
	}
	
	function update_field($id,$field,$value,$anhien=-1) {
	   	$this->update_field_db($this->table,$id,$field,$value,$anhien);
    }
    
    /* 
    hàm update này chỉ dùng để update 1 đối tượng 
    được set dầy dủ các biến luôn cả "id" nhá
    Lưu ý: nếu ko set giá trị dầy đủ thi field nào ko set nó se~ ra null
    cho nen nếu mún update 1 filed thì nen xài hàm update_field() phía trên 
    dể update 1 field nhá  !!!!!!!
    */
	function update() {
		if ($this->id!=NULL && $this->id>0) {
			$list_field = $this->show_field_db($this->table);
			$query = "UPDATE ".$this->table." SET ";
			$flag = 0;
			foreach ($list_field as $field) {
		    	echo $field[0] ;die;
                            if ($flag == 1)
		    		$query = $query.$field[0]."='".$this->$field[0]."',";
                        
		    	$flag = 1;
                        
		    }
                    
		    $query = substr($query,0,strlen($query)-1);
			$query = $query." WHERE id='$this->id'";
			mysql_query($query) or die(mysql_error());
		}
    }
	
	function create() {
		// mún kiểm tra gì thì viết ở đây
		
		// code cứng không cần sửa **********************
		if ($this->thutu==-1)
			$this->thutu = $this->get_max_thutu_db($this->table)+1;
		$list_field = $this->show_field_db($this->table);
    	$query = "INSERT INTO ".$this->table." (";
    	$flag = 0;
    	foreach ($list_field as $field) {
    		if ($flag == 1)
    			$query = $query.$field[0].",";
    		$flag = 1;
    	}
    	$query = substr($query,0,strlen($query)-1);
    	$query = $query.") VALUES (";
    	$flag = 0;
    	foreach ($list_field as $field) {
    		if ($flag == 1)
    			$query = $query."'".$this->$field[0]."',";
    		$flag = 1;
    	}
    	$query = substr($query,0,strlen($query)-1);
    	$query = $query.")";
		mysql_query($query) or die(mysql_error());
		// ***********************************************
    }
	
	
	
	// A E nào mún viết hàm mới thì dựa vào 2 demo dưới day nhá
	//TRuy van list du~ lieu
	function ham_demo($ten) {
		$query = "SELECT * FROM ".$this->table." WHERE ten_vi like '%".$ten."%' order by ngay DESC";
		return $this->get_list_data_db($query);
	}
	//TRuy van 1 phan tu
	function ham_demo2($id) {
		$query = "SELECT * FROM ".$this->table." WHERE id='$id'";
		return $this->get_data_db($query);
	}
	//Thuc hien 1 xử lý
	function ham_demo3($id) {
		$query = "DELETE FROM ".$this->table." WHERE id='$id'";
		mysql_query($query) or die(mysql_error());
	}
	
}



?>